#!/usr/bin/env python
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
import smtplib
import datetime
import time
from qb_database_ import instance
q = instance()

msg_to = ['chenqx@base-fx.com', 'pranjan@base-fx.com','james.xiao@base-fx.com','wangdh@base-fx.com','mengyan1@base-fx.com', 'adeosthale@base-fx.com']


def AutoSenMaile(content_text, content_img, subject):
    localtime_m = time.strftime("%H", time.localtime())
    y = datetime.datetime.today() - datetime.timedelta(days=1)
    time_now_ = datetime.datetime.now().strftime('%Y-%m-%d')
    time_y = y.strftime('%Y-%m-%d')
    t3 = time.strftime("%Y-%m-%d", time.localtime())
    t3_y = y.strftime("%Y-%m-%d")
    msg = MIMEMultipart()
    content_text = MIMEText(content_text,_subtype='plain',_charset='utf-8')
    msg.attach(content_text)

    image_data = MIMEImage(open(content_img, 'rb').read())
    image_data.add_header('Content_ID', '<image>')
    msg.attach(image_data)

    msg['From'] = 'chenqx@base-fx.com'
    msg['To'] = ','.join(msg_to)
    msg['Subject'] = '%s'%subject


    server = smtplib.SMTP()
    server.connect('imap.base-fx.com')
    server.login('chenqx@base-fx.com','587hh*12')
    server.sendmail(msg['From'],msg['To'].split(','),msg.as_string())
    server.quit()


def oliver_need():
    import qb_database_
    qd = qb_database_.instance()
    Base_available = qd.host_base_run() + qd.host_base_idle()
    Node_available = qd.host_node_run() + qd.host_node_idle()
    Base_lock = qd.host_base_lock() + qd.host_base_down()
    node_lock = qd.host_node_lock() + qd.host_node_down()
    print Base_lock,node_lock
    return Base_available,Node_available,Base_lock,node_lock


if __name__ == '__main__':
    while True:
        current_time = time.localtime(time.time())
        # if (((current_time.tm_wday == 4) or (current_time.tm_wday == 5)) and (current_time.tm_hour == 22) and (current_time.tm_min == 10) and (current_time.tm_sec == 0)):
        #     b,n,base_lock,node_lock = oliver_need()
        #     print 'send email is ok'
        #     txt1 = 'Hi~\n'
        #     txt2 = 'The machine usage now as follows(>48G):\n'
        #     txt3 = '10:00pm we had open the Base_machine:  %s  and  there are %s Base_machine lock and down.\n' % (b,base_lock)
        #     txt4 = '10:00pm we just open the Node_machine:  %s and  there are %s node_machine lock and down.\n' % (n,node_lock)
        #
        #     content = txt1 + txt2 + txt3 + txt4
        #     subject = 'Render farm : Workstation situation'
        #     AutoSenMaile(content,'',subject)
        if (((current_time.tm_wday == 0) and (current_time.tm_hour == 9) and (current_time.tm_min == 40) and (
                    current_time.tm_sec == 0))):
            import get_the_week_date_new
            get_the_week_date_new.main(7)
            data_now = datetime.datetime.now().strftime("%Y-%m-%d")
            content_path = '/show/WDN/misc/dept/lgt/RenderFarm/archive/Classification_report/daily_report/week_pic/'+"Machine_Usage[%s].jpg" % (data_now[5:])
            subject = 'Weekly(Day7) : MR and Katana machine useage'
            AutoSenMaile('',content_path, subject)
        elif (((current_time.tm_wday == 4) and (current_time.tm_hour == 18) and (current_time.tm_min == 15) and (
            current_time.tm_sec == 0))):
            import get_the_week_date_new
            get_the_week_date_new.main(4)
            data_now = datetime.datetime.now().strftime("%Y-%m-%d")
            content_path = '/show/WDN/misc/dept/lgt/RenderFarm/archive/Classification_report/daily_report/week_pic/' + "Machine_Usage[%s].jpg" % (
            data_now[5:])
            subject = 'Weekly(Day5) : MR and Katana machine useage'
            AutoSenMaile('', content_path, subject)

        time.sleep(1)

